package com.newtouch.bxzs.database.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.CooperationCompanyOrganization;
import com.newtouch.bxzs.common.base.model.Organization;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import com.newtouch.bxzs.database.mapper.CooperationCompanyOrganizationMapper;
import com.newtouch.bxzs.database.mapper.OrganizationMapper;
import com.newtouch.bxzs.database.model.*;
import com.newtouch.bxzs.database.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 实现组织架构列表查看和详情查看
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020/12/21 15:36</td>
 * <td>linhongxi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author linhongxi
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private CooperationCompanyOrganizationMapper cooperationCompanyOrganizationMapper;

    /**
     * 根据机构名称查询机构信息
     * com.newtouch.bxzs.database.service.OrganizationService#getOrganizationList
     * @param  organizationVO 机构名称
     * @return IPage<OrganizationBO>
     */
    @Override
    public IPage<OrganizationBO> getOrganizationListByPage(OrganizationVO organizationVO) {
        QueryWrapper<Organization> queryWrapper=new QueryWrapper<>();
        //queryWrapper.eq("ORG_PCODE",organizationVO.getOrgCode()).orderByAsc("ORG_CODE");
        if (!StringUtils.isEmpty(organizationVO.getOrganizationName())) {
            /* 根据查询条件生成QueryWrapper，使用eq方法拼接查询条件，先写字段名，再写参数 */
            queryWrapper.like("ORGANIZATION_NAME",organizationVO.getOrganizationName());
        }
        //分页
        Page<Organization> page = new Page<>(organizationVO.getCurrentPage(), organizationVO.getPageSize());
        queryWrapper.eq("IS_ENABLE",1);
        queryWrapper.eq("IS_DELETE",0);
        queryWrapper.orderByAsc("ORGANIZATION_CODE","CREATE_TIME");
        IPage<Organization> OrganizationsParentByPage =  organizationMapper.selectPage(page,queryWrapper);
        /* 接口返回参数一律使用BO返回，不允许使用实体直接返回 */
        if (OrganizationsParentByPage == null || CollectionUtil.isEmpty(OrganizationsParentByPage.getRecords())) {
            return null;
        }
        List<OrganizationBO> organizationBOs = EntityToBeanUtils.copyBean(OrganizationsParentByPage.getRecords(),
                OrganizationBO.class);
        IPage<OrganizationBO> organizationBOList = new Page<>();
        organizationBOList.setRecords(organizationBOs);
        organizationBOList.setCurrent(OrganizationsParentByPage.getCurrent());
        organizationBOList.setPages(OrganizationsParentByPage.getPages());
        organizationBOList.setSize(OrganizationsParentByPage.getSize());
        organizationBOList.setTotal(OrganizationsParentByPage.getTotal());
        return organizationBOList;
    }

    /**
     * 根据实时查询机构信息
     * com.newtouch.bxzs.database.service.OrganizationService#getOrganizationList
     * @param organizationListVO 机构列表
     * @return List<OrganizationBO>
     */
    @Override
    public List<OrganizationBO> getOrganizationList(OrganizationListVO organizationListVO) {

        return organizationMapper.organizationByChild(organizationListVO.getOrganizationCode());
    }

    /**
     * 根据机构主键查询机构信息
     * com.newtouch.bxzs.database.service.OrganizationService#getOrganizationInfo
     * @param organizationId 机构ID
     * @return OrganizationBO
     */
    @Override
    public OrganizationBO getOrganizationInfo(String organizationId) {
        if (StringUtils.isEmpty(organizationId)) {
            /* 没有入参的时候，直接返回空 */
            throw new BaseException(BaseErrorEnum.BODY_NOT_MATCH);
        }
            /* 查询机构详情以及上级机构名称 */
        return  organizationMapper.organizationInfo(organizationId);
    }

    /**
     * 获取保险机构列表信息
     * com.newtouch.bxzs.database.service.OrganizationService#InsuranceList
     * @param   cooperationCompanyOrganizationVO 对端机构
     * @return IPage<CooperationCompanyOrganizationBO>
     */
    @Override
    public IPage<CooperationCompanyOrganizationBO> insuranceList(CooperationCompanyOrganizationVO cooperationCompanyOrganizationVO) {
        QueryWrapper<CooperationCompanyOrganization> queryWrapper = new QueryWrapper<>();
        Page<CooperationCompanyOrganization> page = new Page<>(cooperationCompanyOrganizationVO.getCurrentPage(), cooperationCompanyOrganizationVO.getPageSize());
        if (!StringUtils.isEmpty(cooperationCompanyOrganizationVO.getCooperationCompanyOrganizationName())) {
            queryWrapper.like("COOPERATION_COMPANY_ORGANIZATION_NAME", cooperationCompanyOrganizationVO.getCooperationCompanyOrganizationName());
        }
        queryWrapper.eq("IS_ENABLE",1);
        queryWrapper.eq("IS_DELETE", 0);
        queryWrapper.eq("DATA_SOURCE", cooperationCompanyOrganizationVO.getDataSource());
        IPage<CooperationCompanyOrganization> insuranceList = cooperationCompanyOrganizationMapper.selectPage(page, queryWrapper);
        if (insuranceList == null || CollectionUtil.isEmpty(insuranceList.getRecords())) {
            return null;
        }
        /* 接口返回参数一律使用BO返回，不允许使用实体直接返回 */
        List<CooperationCompanyOrganizationBO> insuranceBOs = EntityToBeanUtils.copyBean(insuranceList.getRecords(),
                CooperationCompanyOrganizationBO.class);
        IPage<CooperationCompanyOrganizationBO> insuranceBOList = new Page<>();
        insuranceBOList.setRecords(insuranceBOs);
        insuranceBOList.setSize(insuranceList.getSize());
        insuranceBOList.setCurrent(insuranceList.getCurrent());
        insuranceBOList.setPages(insuranceList.getPages());
        insuranceBOList.setTotal(insuranceList.getTotal());
        return insuranceBOList;
    }

    /**
     * 实时查询合作公司机构信息
     * com.newtouch.bxzs.database.service.OrganizationService#getOrganizationList
     * @param cooperationCompanyOrganizationListVO 对端机构
     * @return List<CooperationCompanyOrganizationBO>
     */
    @Override
    public List<CooperationCompanyOrganizationBO> getCooperationCompanyOrganizationList(CooperationCompanyOrganizationListVO cooperationCompanyOrganizationListVO) {

        return  cooperationCompanyOrganizationMapper.CooperationCompanyOrganizationByChild(cooperationCompanyOrganizationListVO.getCooperationCompanyOrganizationCode());
    }

    /**
     * 营销活动或者营销会议根据用户机构编码实时查询机构信息
     *
     * @param
     * @return List<OrganizationBO>
     */
    @Override
    public List<OrganizationBO> getOrganizationListByCode() {
        // 获取当前登录用户机构编码
        String userOrganizationCode = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserOrganizationCode();
        // 查询当前登录用户下级机构
        return  organizationMapper.organizationByUserCode(userOrganizationCode);
    }
}
